﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;

namespace CaoNiMaCoolPad
{
    public partial class Form1
    {
        private int currentMessageIndex = 0;

        private void nameOffestBox_ValueChanged(object sender, EventArgs e)
        { nameTextBox.Text = entryListBox.Items[(int)(ignoreHeadBox.Value + nameOffestBox.Value)].ToString(); }
        private void phoneNumberOffestBox_ValueChanged(object sender, EventArgs e)
        { phoneTextBox.Text = entryListBox.Items[(int)(ignoreHeadBox.Value + phoneNumberOffestBox.Value)].ToString(); }

        private void dateOffestBox_ValueChanged(object sender, EventArgs e)
        { dateTextBox.Text = entryListBox.Items[(int)(ignoreHeadBox.Value + dateOffestBox.Value)].ToString(); }

        private void folderOffestBox_ValueChanged(object sender, EventArgs e)
        { folderTextBox.Text = entryListBox.Items[(int)(ignoreHeadBox.Value + folderOffestBox.Value)].ToString(); }

        private void contentOffestBox_ValueChanged(object sender, EventArgs e)
        { contentTextBox.Text = entryListBox.Items[(int)(ignoreHeadBox.Value + contentOffestBox.Value)].ToString(); }

        private void RefreshPreview()
        {
            int length = (int)messageEntriesBox.Value;
            nameTextBox.Text = entryListBox.Items[(int)(ignoreHeadBox.Value + currentMessageIndex * length + nameOffestBox.Value)].ToString();
            phoneTextBox.Text = entryListBox.Items[(int)(ignoreHeadBox.Value + currentMessageIndex * length + phoneNumberOffestBox.Value)].ToString();
            dateTextBox.Text = entryListBox.Items[(int)(ignoreHeadBox.Value + currentMessageIndex * length + dateOffestBox.Value)].ToString();
            folderTextBox.Text = entryListBox.Items[(int)(ignoreHeadBox.Value + currentMessageIndex * length + folderOffestBox.Value)].ToString();
            int contentIndex = (int) (ignoreHeadBox.Value + currentMessageIndex*length + contentOffestBox.Value);
            contentTextBox.Text = entryListBox.Items[contentIndex].ToString();

            entryListBox.SelectedIndex = contentIndex;

        }

        private void previewPreviousButton_Click(object sender, EventArgs e)
        {
            currentMessageIndex--;
            RefreshPreview();
            if (currentMessageIndex == 0)
                previewPreviousButton.Enabled = false;
        }

        private void previewNextButton_Click(object sender, EventArgs e)
        {
            currentMessageIndex++;
            RefreshPreview();

            previewPreviousButton.Enabled = true;
            if (currentMessageIndex == MaxMessage())
                previewNextButton.Enabled = false;

        }

        private void previewLastButton_Click(object sender, EventArgs e)
        {
            currentMessageIndex = MaxMessage();
            RefreshPreview();

            previewPreviousButton.Enabled = true;
            previewNextButton.Enabled = false;
        }

        private void previewFirstButton_Click(object sender, EventArgs e)
        {
            currentMessageIndex = 0;
            RefreshPreview();

            previewNextButton.Enabled = true;
            previewPreviousButton.Enabled = false;
        }

        /// <summary>获取MessageIndex的最大值</summary>
        private int MaxMessage()
        {
            return (int)Math.Floor((entryListBox.Items.Count - ignoreHeadBox.Value - ignoreTailBox.Value)
                                            / messageEntriesBox.Value) - 1;
        }
    }
}
